// main graphics method
function render() {

	// (posx, posy) should be at (iViewCanvasWidth/2, iViewCanvasHeight - iViewYOffset) on view canvas
	oViewCanvas.width = oViewCanvas.width;
	oViewCtx.fillStyle = "green";
	oViewCtx.fillRect(0,0,oViewCanvas.width,oViewCanvas.height);

	oViewCtx.save();
	oViewCtx.translate(iViewCanvasWidth/2,iViewCanvasHeight-iViewYOffset);
	oViewCtx.rotate((180 + oPlayer.rotation) * Math.PI / 180);
	oViewCtx.drawImage(
		oMapImg,
		-oPlayer.x,-oPlayer.y
	);

	oViewCtx.restore();

	oScreenCanvas.width=oScreenCanvas.width;
	oScreenCtx.fillStyle = "green";
	//oScreenCtx.fillRect(0,0,oScreenCanvas.width,oScreenCanvas.height);

	for (var i=0;i<aStrips.length;i++) {

		var oStrip = aStrips[i];

		if (iRenderMode == 0) {
			try {
			oScreenCtx.drawImage(
				oViewCanvas,
				iViewCanvasWidth/2 - (oStrip.stripwidth/2),
				//Math.floor(((iViewCanvasHeight-iViewYOffset) - oStrip.mapz)),
				((iViewCanvasHeight-iViewYOffset) - oStrip.mapz)-1,
				oStrip.stripwidth,
				oStrip.mapzspan,

				0,(iHeight-oStrip.viewy)/fLineScale,iWidth/fLineScale,1
			);
			} catch(e) {};

		}

		if (iRenderMode == 1) {
			var iStripHeight = Math.max(3,oStrip.mapzspan);
			//oStrip.canvas.width=oStrip.canvas.width;
			oStrip.canvas.height = iStripHeight;
			oStrip.canvas.getContext("2d").clearRect(0,0,oStrip.stripwidth,1);
			try {
			oStrip.canvas.getContext("2d").drawImage(
				oViewCanvas,
				iViewCanvasWidth/2 - (oStrip.stripwidth/2),
				((iViewCanvasHeight-iViewYOffset) - oStrip.mapz)-1,
				oStrip.stripwidth,
				oStrip.mapzspan,

				0,0,oStrip.stripwidth,iStripHeight
			);
			} catch(e) {};
		}

	}

	var iOffsetX = (iWidth/2)*iScreenScale;
	var iOffsetY = (iHeight - iViewYOffset)*iScreenScale;
	
	//oPlayer.sprite.draw(iOffsetX,iOffsetY,1);
//		console.log("x: " + oPlayer.x + "; y: " + oPlayer.y);

	for (var i=0;i<aKarts.length;i++) {
		var oKart = aKarts[i];
		if (oKart.cpu) {

			var fCamX = -(oPlayer.x - oKart.x);
			var fCamY = -(oPlayer.y - oKart.y);

			var fRotRad = oPlayer.rotation * Math.PI / 180;

			var fTransX = fCamX * Math.cos(fRotRad) - fCamY * Math.sin(fRotRad);
			var fTransY = fCamX * Math.sin(fRotRad) + fCamY * Math.cos(fRotRad);

			var iDeltaY = -iCamHeight;
			var iDeltaX = iCamDist + fTransY;

			var iViewY = ((iDeltaY / iDeltaX) * iCamDist + iCamHeight) - iViewHeight;
			var fViewX = -(fTransX / (fTransY + iCamDist)) * iCamDist;

			var fAngle = oPlayer.rotation - oKart.rotation;
			while (fAngle < 0)
				fAngle += 360;
			while (fAngle > 360)
				fAngle -= 360;

			var iAngleStep = Math.round(fAngle / (360 / 22));
			if (iAngleStep == 22) iAngleStep = 0;

			oKart.sprite.setState(iAngleStep);

			oKart.sprite.div.style.zIndex = Math.round(10000 - fTransY);

			oKart.sprite.draw(
				((iWidth/2) + fViewX) * iScreenScale, 
				(iHeight - iViewY) * iScreenScale,
				fFocal / (fFocal + (fTransY))
			);

		}
	}

	oPlayer.sprite.div.style.zIndex = 10000;
	oPlayer.sprite.draw(iOffsetX,iOffsetY,1);

	oHills.draw(oPlayer.rotation);
	oTrees.draw(oPlayer.rotation);
}


function ai(oKart) {
	var aCurPoint = oMap.aipoints[oKart.aipoint];

	// first time, get the point coords
	if (!oKart.aipointx)
		oKart.aipointx = aCurPoint[0];
	if (!oKart.aipointy)
		oKart.aipointy = aCurPoint[1];

	var iLocalX = oKart.aipointx - oKart.x;
	var iLocalY = oKart.aipointy - oKart.y;

	iRotatedX = iLocalX * Math.cos(oKart.rotation * Math.PI / 180) - iLocalY * Math.sin(oKart.rotation * Math.PI / 180);
	iRotatedY = iLocalX * Math.sin(oKart.rotation * Math.PI / 180) + iLocalY * Math.cos(oKart.rotation * Math.PI / 180);

	var fAngle = Math.atan2(iRotatedX,iRotatedY) / Math.PI * 180;

	if (Math.abs(fAngle) > 10) {
		if (oKart.speed == fMaxSpeed) oKart.speedinc = -0.5;
		oKart.rotincdir = fAngle > 0 ? 1 : -1;
	} else {
		oKart.rotincdir = 0;
	}

	oKart.speedinc = 1;

	var fDist = Math.sqrt(iLocalX*iLocalX + iLocalY*iLocalY);
	if (fDist < 40) {
		oKart.aipoint++;
		if (oKart.aipoint >= oMap.aipoints.length)
			oKart.aipoint = 0;

		var oNewPoint = oMap.aipoints[oKart.aipoint];
		oKart.aipointx = oNewPoint[0] + (Math.random()-0.5) * 10;
		oKart.aipointy = oNewPoint[1] + (Math.random()-0.5) * 10;
	}
}

document.onkeydown = function(e) {
	if (!bRunning) return;
	switch (e.keyCode) {
		case 38: // up
			oPlayer.speedinc = 1;
			break;
		case 37: // left
			oPlayer.rotincdir = 1;
			break;
		case 39: // right
			oPlayer.rotincdir = -1;
			break;
		case 40: // down
			oPlayer.speedinc -= 0.2;
			break;
	}
}

document.onkeyup = function(e) {
	if (!bRunning) return;
	switch (e.keyCode) {
		case 38: // up
			oPlayer.speedinc = 0;
			break;
		case 37: // left
			oPlayer.rotincdir = 0;
			break;
		case 39: // right
			oPlayer.rotincdir = 0;
			break;
		case 40: // down
			oPlayer.speedinc = 0;
			break;
	}
}